﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.auth.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;
import static com.hh.bss.util.extjs.JsonHelper.outJsonArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.auth.dao.*;
import com.hh.bss.auth.entity.*;
import com.hh.bss.auth.service.*;
import com.hh.bss.auth.vo.query.*;
import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.entity.Company;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.JsonHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class SystemBranchDeptController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private SystemBranchDeptService systemBranchDeptService;
	
	private SystemBranchDept systemBranchDept;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setSystemBranchDeptServiceImpl(SystemBranchDeptService service) {
		this.systemBranchDeptService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	public void parenttitlelist(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		
		String companyId=request.getParameter("companyId");
		StringBuffer sql = new StringBuffer();
		List values = new ArrayList();
		sql.append(" select d2.id,d2.name from system_branch_dept d2 where exists(select 1 from (select d1.parent_ids from system_branch_dept d1 where d1.branch_company_id in(select c1.parent_id from system_branch_company c1 where c1.id=?)) aa where aa.parent_ids like concat(d2.parent_ids,'%')) ");
		values.add(StringHelper.parseInteger(companyId, 0));
		
		List l=systemBranchDeptService.getEntityDao().getJdbcDao().findByQuery(sql.toString(),values.toArray(), new MapObjectRowMapper());
		
		List lCompany=systemBranchDeptService.getEntityDao().getHibernateTemplate().find("from SystemBranchCompany t where t.id=? ", StringHelper.parseInteger(companyId, 0));
		if (lCompany!=null&&lCompany.size()>0){
			SystemBranchCompany systemBranchCompany=(SystemBranchCompany)lCompany.get(0);
			if (systemBranchCompany.getParentId()!=null&&systemBranchCompany.getParentId().intValue()==0){
				if (l==null||l.size()<1){
					Map m=new HashMap();
					m.put("id", new Integer(0));
					m.put("name", "根部门");
					
					l.add(m);
				}
			}
		}
		//如果是根公司，则可以加
		
		
		JsonHelper.outJsonString(response,JSONArray.fromObject(l).toString());
	}

	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,SystemBranchDeptQuery systemBranchDeptQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = systemBranchDeptService.findPage(systemBranchDeptQuery);
		
		List<SystemBranchDept> SystemBranchDeptlist = (List) page.getResult();
		ListRange<SystemBranchDept> resultList = new ListRange<SystemBranchDept>();
		resultList.setRows(SystemBranchDeptlist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	public void deptlist(HttpServletRequest request,HttpServletResponse response,SystemBranchDeptQuery systemBranchDeptQuery) throws IOException
	{
		List deptlist = (List)systemBranchDeptService.findSystemBranchDept(systemBranchDeptQuery);
		outJsonArray(response,deptlist);
		
	}

	public void get(HttpServletRequest request,HttpServletResponse response,SystemBranchDeptQuery systemBranchDeptQuery) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			SystemBranchDept oldSystemBranchDept = (SystemBranchDept)systemBranchDeptService.getById(systemBranchDeptQuery.getId());
			//取父节点名称 
			List pTitle=systemBranchDeptService.getEntityDao().getHibernateTemplate().find("from SystemBranchDept t where t.id=? ", oldSystemBranchDept.getParentId());
			if (pTitle!=null&&pTitle.size()>0){
				SystemBranchDept systemBranchDept=(SystemBranchDept)pTitle.get(0);
				oldSystemBranchDept.setParentName(systemBranchDept.getName());
			}else  oldSystemBranchDept.setParentName("无");
				
			if(oldSystemBranchDept!=null) result.put("data", oldSystemBranchDept);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,SystemBranchDept systemBranchDept) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchDept.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(systemBranchDept,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchDept.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(systemBranchDept,"createDt",new Date());}catch(Exception ex){}
				}

			systemBranchDeptService.save(systemBranchDept);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,SystemBranchDept systemBranchDept) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        SystemBranchDept oldsystemBranchDept = (SystemBranchDept)systemBranchDeptService.getById(systemBranchDept.getId());
                         
			bind(request,oldsystemBranchDept);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchDept.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldsystemBranchDept,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchDept.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldsystemBranchDept,"modifiedDt",new Date());}catch(Exception ex){}
				}


			systemBranchDeptService.update(oldsystemBranchDept);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				systemBranchDeptService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
